from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todolist.settings')  # 必须修改模块名
app = Celery('todolist')  # 可以修改
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
# 增加以下内容配置
app.conf.broker_url = 'redis://192.168.190.128:6379/1'
# 如果超过visibility_timeout，Celery会认为此任务失败
# 会重分配其他worker执行该任务，这样会造成重复执行。visibility_timeout这个值大一些
# 注意，如果慢任务执行时长超过visibility_timeout依然会多执行
app.conf.broker_transport_options = {'visibility_timeout': 43200}  # 12 hours
app.conf.result_backend = 'redis://192.168.190.128:6379/2'  # 执行结果存储
app.conf.update(
    enable_utc=True,
    timezone='Asia/Shanghai'
)

# celery -A todolist worker -P eventlet --loglevel=INFO --concurrency=5 -n worker1@%n
